package com.dangdi.controller;

import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.protocol.HttpContext;

import javax.net.ssl.SSLException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.UnknownHostException;

public class myRetryHandler implements HttpRequestRetryHandler {
    @Override
    public boolean retryRequest(
            IOException exception,
            int executionCount,
            HttpContext context) {
        if (executionCount >= 5) {
            // Do not retry if over max retry count
            System.out.println("第"+executionCount+"次报错");
            return false;
        }
        if (exception instanceof InterruptedIOException) {
            // Timeout
            System.out.println("Timeout 报错了");
            return true;
        }
        if (exception instanceof UnknownHostException) {
            // Unknown host
            System.out.println("Unknown host 报错了");
            return true;
        }
        if (exception instanceof ConnectTimeoutException) {
            // Connection refused
            System.out.println("Connection refused 报错了");
            return true;
        }
        if (exception instanceof SSLException) {
            // SSL handshake exception
            System.out.println("SSL handshake exception 报错了");
            return true;
        }
        return true;
    }
}
